package com.neusoft.Dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.neusoft.entity.User;

public class JDBCTest2 {
	private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
	private static final String  URL = "jdbc:mysql://localhost:3306/test2";
	private static final String USER_NAME = "root";
	private static final String USER_PASSWORD = "mysqladmin";
	private static Connection connection = null;
	static
	{
		try {
			Class.forName(DRIVER_NAME);
			connection = DriverManager.getConnection(URL, USER_NAME, USER_PASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch(SQLException e) {
			e.printStackTrace();
		}
	}
	//tianjia
	public boolean addOneData(User user) {
		PreparedStatement prepareStatement = null;
		String sql = "insert into user (user_name,user_password) values (?,?)" ;
		try {
			prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setString(1, user.getUserName()); 
			prepareStatement.setString(2, user.getUserPassword());
			int rs = prepareStatement.executeUpdate();
			
			if(rs>0)
			{
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally
		{
			
			if(prepareStatement!= null)
			{
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection!= null)
			{
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}
	public boolean addOneUpdate(User user) {
		PreparedStatement prepareStatement = null;
		String sql = "update user set user_password = ? where user_id = ?" ;
		try {
			prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setString(1, user.getUserPassword());
			prepareStatement.setInt(2, user.getUserID());
			int rs = prepareStatement.executeUpdate();
			if(rs>0)
			{
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally
		{
			
			if(prepareStatement!= null)
			{
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection!= null)
			{
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}
	public boolean addOneDelete(User user) {
		PreparedStatement prepareStatement = null;
		String sql = "delete from user where user_id = ?" ;
		try {
			prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setInt(1, user.getUserID());
			int rs = prepareStatement.executeUpdate();
			if(rs>0)
			{
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally
		{
			
			if(prepareStatement!= null)
			{
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection!= null)
			{
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}
}
